package org.dylan.demo.filter;

import java.io.IOException;
import java.util.BitSet;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.Filter;
import org.apache.lucene.util.DocIdBitSet;

public class BookCatagoryFilter extends Filter {

	private static final long serialVersionUID = 5403388975173863488L;

	private String catagory = "";

	public BookCatagoryFilter(String catagory) {
		this.catagory = catagory;
	}

	private String catagoryPrograming[] = new String[] { "10", "11", "12",
			"13", "14" };
	private String catagorySoftEnginee[] = new String[] { "15", "16" };

	@Override
	public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
		final BitSet bits = new BitSet(reader.maxDoc());
		String catagorys[] = null;
		if ("6".equals(catagory)) {// 编程语言
			catagorys = catagoryPrograming;
		} else if ("7".equals(catagory)) {//软件工程
			catagorys = catagorySoftEnginee;
		} else if ("5".equals(catagory)) {//计算机与网络
			catagorys = new String[catagoryPrograming.length + catagorySoftEnginee.length];
			System.arraycopy(catagoryPrograming, 0, catagorys, 0, catagoryPrograming.length);
			System.arraycopy(catagorySoftEnginee, 0, catagorys, catagoryPrograming.length, catagorySoftEnginee.length);
		}
		if (catagorys != null) {
			for (String catagory : catagorys) {
				Term term = new Term("catagory.id", catagory);
				TermDocs termDocs = reader.termDocs(term);
				while (termDocs.next()) {
					bits.set(termDocs.doc());
				}
			}
		}
		return new DocIdBitSet(bits);

	}

}
